package com.good.child.ui.activity.ai;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import com.good.child.MainActivity;
import com.good.child.R;
import com.good.child.bean.ai.CommunityBean;
import com.good.child.iview.CommunityIView;
import com.good.child.pressent.CommunityPresenter;
import com.good.child.ui.adapter.ai.AiCommunityAdapter;
import com.good.child.widgets.dialog.PreviewDialog;
import com.yimidida.library.base.BaseActivity;
import com.yimidida.library.tool.TActivityUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * AI社区
 */
public class AiCommunityActivity extends BaseActivity<CommunityIView, CommunityPresenter> implements CommunityIView {
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    private AiCommunityAdapter aiCommunityAdapter;
    private List<CommunityBean> dataList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ai_community);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected CommunityPresenter initPresenter() {
        return new CommunityPresenter(this);
    }
    List<String> imageList;
    @Override
    protected void initView() {
        setStatusBar(false, true, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setAndroidNativeLightStatusBar(this, true);
        setTitle("欢乐社区");
        dataList = new ArrayList<>();
        /*CommunityBean testBean;
        for (int i = 20; i > 0; i--) {
            testBean = new CommunityBean();
            testBean.setContent("AiCommunityAdapter.ITEM_TYPE_1");
            dataList.add(testBean);
        }*/

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        aiCommunityAdapter = new AiCommunityAdapter(this);
        aiCommunityAdapter.setOnItemClick(itemClick);
        recyclerView.setAdapter(aiCommunityAdapter);
        aiCommunityAdapter.setData(dataList);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getCommunityList();
    }

    private AiCommunityAdapter.onItemClick itemClick = new AiCommunityAdapter.onItemClick() {
        @Override
        public void onClickView(CommunityBean bean) {
            if (bean == null || TextUtils.isEmpty(bean.getImageUrl())) {
                return;
            }
            List<String> paths = new ArrayList<>();
            if (bean.getImageUrl().contains(";")) {
                String[] split = bean.getImageUrl().split(";");
                if (split == null || split.length == 0) {
                    return;
                }
                paths.addAll(Arrays.asList(split));
            } else {
                paths.add(bean.getImageUrl());
            }
            PreviewDialog.showDialog(getSupportFragmentManager(), paths,true);
        }
    };

    @OnClick({R.id.btn_community_send,R.id.btn_my_community})
    public void onClickView(View view) {
        switch (view.getId()) {
            case R.id.btn_community_send:
                TActivityUtils.jumpToActivity(AiCommunityActivity.this, SelectImageActivity.class);
                break;
            case R.id.btn_my_community:
                TActivityUtils.jumpToActivity(AiCommunityActivity.this, AiCommunityDetailActivity.class);
                break;
        }
    }


    private void getCommunityList() {
        String userId="1";
        mPresenter.getList(userId);
    }

    @Override
    public void getList(CommunityBean communityBean) {
        dataList.clear();
        if (communityBean.rows!=null){
            dataList.addAll(communityBean.rows);
            aiCommunityAdapter.notifyDataSetChanged();
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_HOME) {
            TActivityUtils.jumpToActivity(getBaseContext(), MainActivity.class);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}